package com.personal.service.impl;

import com.personal.dao.HousePropDao;
import com.personal.domain.HouseProp;
import com.personal.domain.HousePropPlus;
import com.personal.service.HousePropService;
import com.personal.util.partDisplay.HousePropPart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @projectName: smartCommunity_springboot
 * @package: com.personal.service.impl
 * @className: HousePropServiceImpl
 * @author: bong~
 * @description: TODO
 * @date: 2023/8/10 17:00
 * @version: 1.0
 */


@Service
public class HousePropServiceImpl implements HousePropService {


    @Autowired
    private HousePropDao housePropDao;

    @Override
    public List<HouseProp> selectAll() {
        return housePropDao.selectAll();
    }

    @Override
    public List<HousePropPart> selectPartAll() {
        return housePropDao.selectPartAll();
    }

    @Override
    public List<HousePropPart> selectPartByKey(String name, String type) {
        return housePropDao.selectPartByKey(name,type);
    }

    @Override
    public boolean add(HousePropPlus housePropPlus) {

        for(int parkAreaId:housePropPlus.getParkAreaIds()){
            HouseProp cur=new HouseProp(parkAreaId);
            cur.setCustomerName(housePropPlus.getCustomerName());
            cur.setCustomerType(housePropPlus.getCustomerType());
            cur.setOwner(housePropPlus.getOwner());
            cur.setObjectType(housePropPlus.getObjectType());
            cur.setTelephone(housePropPlus.getTelephone());
            cur.setHistoricalCustomer(housePropPlus.getHistoricalCustomer());
            cur.setFromContract(housePropPlus.getFromContract());
            cur.setRemark(housePropPlus.getRemark());
            cur.setStartChargingDate(housePropPlus.getStartChargingDate());
            cur.setEndChargingDate(housePropPlus.getEndChargingDate());
            cur.setHandHouseDate(housePropPlus.getHandHouseDate());
            cur.setStartLeaseDate(housePropPlus.getStartLeaseDate());
            cur.setEndLeaseDate(housePropPlus.getEndLeaseDate());
            cur.setSignDate(housePropPlus.getSignDate());
            cur.setTransferDate(housePropPlus.getTransferDate());
            cur.setImportDate(housePropPlus.getImportDate());
//          ------------------------------------------------------------------

            int flag1=housePropDao.add(cur);
            int flag2=housePropDao.upStatusParkArea(parkAreaId);
            int flag3=housePropDao.upRelHouseProp(housePropPlus.getCustomerId(),housePropPlus.getCustomerType(),parkAreaId);

            if(flag1<0||flag2<0||flag3<0)
                return true;
        }
        return true;
    }

    @Override
    public HouseProp getUpdateData(int id) {
        return housePropDao.getUpdateData(id);
    }

    @Override
    public boolean update(HouseProp houseProp) {
        int flag=housePropDao.update(houseProp);
        if(flag>0)
            return true;
        return false;
    }

    @Override
    public boolean delete(List<Integer> ids) {

        for(int id:ids){
            int parkAreaId=housePropDao.getParkAreaIdById(id);
            int flag1=housePropDao.upStatusParkArea(parkAreaId);
            int flag2=housePropDao.delete(id);
            int flag3=housePropDao.deleteRelHouseProp(parkAreaId);
            if(flag1<0||flag2<0||flag3<0){
                return false;
            }
        }
        return true;
    }


    @Override
    public List<HousePropPart> selectByAreaIds(List<Integer> parkAreaIds) {

        List<HousePropPart> list=new ArrayList<>();

        for(Integer parkAreaId:parkAreaIds){

            HousePropPart cur=housePropDao.selectByAreaId(parkAreaId);
            //TODO 补充所属园区
            list.add(cur);
        }
        return list;
    }
}
